<template>
  <div class="msgtemplate-container">
    <!-- 搜索工作栏 -->
    <ContentWrap class="search-wrapper">
      <!-- 搜索头部 -->
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:document" />
        </div>
        <div class="header-content">
          <h3 class="search-title">消息模板管理</h3>
          <p class="search-subtitle">管理企业微信消息模板和附件内容</p>
        </div>
      </div>

      <!-- 搜索表单 -->
      <div class="search-form-container">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="100px"
          label-position="left"
          class="enhanced-search-form"
        >
          <div class="form-grid">
            <el-form-item label="模板标题" prop="title" class="form-item">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入模板标题"
                clearable
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:document" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime" class="form-item">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                class="enhanced-date-picker"
              />
            </el-form-item>
          </div>

          <div class="form-actions">
            <el-button @click="handleQuery" class="search-btn">
              <Icon icon="ep:search" class="btn-icon" />
              搜索
            </el-button>
            <el-button @click="resetQuery" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" />
              重置
            </el-button>
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['wecom:msg-template:create']"
              style="border-radius: 4px"
            >
              <Icon icon="ep:plus" class="btn-icon" />
              新增模板
            </el-button>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap class="table-wrapper">
      <!-- 表格头部 -->
      <div class="table-header">
        <div class="header-icon">
          <Icon icon="ep:list" />
        </div>
        <div class="header-content">
          <h4 class="table-title">模板列表</h4>
          <p class="table-subtitle">共 {{ total }} 个模板</p>
        </div>
      </div>

      <!-- 表格容器 -->
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column label="模板标题" align="center" prop="title" min-width="150">
            <template #default="scope">
              <div class="title-cell">
                <Icon icon="ep:document" class="title-icon" />
                <span class="title-text">{{ scope.row.title }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="模板内容" align="center" prop="content" min-width="200">
            <template #default="scope">
              <div class="content-cell">
                <el-tooltip :content="scope.row.content" placement="top">
                  <span class="content-text">{{ scope.row.content }}</span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="附件类型" prop="attachmentType" width="120" align="center">
            <template #default="scope">
              <el-tag
                :type="getAttachmentTypeTag(scope.row.attachmentType).type"
                class="attachment-type-tag"
              >
                <Icon
                  :icon="getAttachmentTypeTag(scope.row.attachmentType).icon"
                  class="tag-icon"
                />
                {{ getAttachmentTypeTag(scope.row.attachmentType).text }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="附件内容" min-width="300">
            <template #default="scope">
              <div class="attachment-cell">
                <!-- 无附件 -->
                <div v-if="scope.row.attachmentType == 0" class="no-attachment">
                  <Icon icon="ep:minus" class="no-attachment-icon" />
                  <span>无附件</span>
                </div>

                <!-- 图片附件 -->
                <div v-else-if="scope.row.attachmentType == 1" class="image-attachment">
                  <div class="image-gallery">
                    <div v-for="(item, index) in scope.row.imgUrl" :key="index" class="image-item">
                      <el-image
                        :src="item"
                        fit="cover"
                        class="attachment-image"
                        :preview-src-list="scope.row.imgUrl"
                        loading="lazy"
                      >
                        <template #error>
                          <div class="image-error">
                            <Icon icon="ep:picture" />
                          </div>
                        </template>
                      </el-image>
                    </div>
                  </div>
                </div>

                <!-- 链接附件 -->
                <div v-else-if="scope.row.attachmentType == 2" class="link-attachment">
                  <div class="link-preview">
                    <div class="link-image">
                      <el-image :src="scope.row.linkPicUrl" fit="cover" class="link-pic">
                        <template #error>
                          <div class="image-error">
                            <Icon icon="ep:link" />
                          </div>
                        </template>
                      </el-image>
                    </div>
                    <div class="link-info">
                      <div class="link-title">
                        <Icon icon="ep:link" class="link-icon" />
                        <span>{{ scope.row.linkTitle || '未命名链接' }}</span>
                      </div>
                      <div class="link-desc">
                        <Icon icon="ep:document" class="desc-icon" />
                        <span>{{ scope.row.linkDesc || '暂无描述' }}</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180"
          >
            <template #default="scope">
              <div class="time-cell">
                <Icon icon="ep:clock" class="time-icon" />
                <span>{{ dateFormatter(scope.row, null, scope.row.createTime) }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="180" fixed="right">
            <template #default="scope">
              <div class="action-buttons">
                <el-button
                  link
                  type="primary"
                  @click="openForm('update', scope.row.id)"
                  v-hasPermi="['wecom:msg-template:update']"
                  class="edit-btn"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>
                <el-button
                  link
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  v-hasPermi="['wecom:msg-template:delete']"
                  class="delete-btn"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <MsgTemplateForm ref="formRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as MsgTemplateApi from '@/api/wecom/msgtemplate'
import MsgTemplateForm from './MsgTemplateForm.vue'

defineOptions({ name: 'MsgTemplate' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: null,
  content: null,
  sort: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

// 获取附件类型标签信息
const getAttachmentTypeTag = (type: number) => {
  const typeMap = {
    0: { text: '无附件', type: 'info', icon: 'ep:minus' },
    1: { text: '图片', type: 'success', icon: 'ep:picture' },
    2: { text: '链接', type: 'primary', icon: 'ep:link' }
  }
  return typeMap[type] || typeMap[0]
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MsgTemplateApi.getMsgTemplatePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MsgTemplateApi.deleteMsgTemplate(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.msgtemplate-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.search-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;
    }
  }
}

.enhanced-input,
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.form-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.search-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.reset-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.add-btn {
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.table-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  .header-icon {
    width: 40px;
    height: 40px;
    background: $primary-color;
    border-radius: $border-radius-small;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      width: 36px;
      height: 36px;
      font-size: 16px;
    }
  }

  .header-content {
    flex: 1;
    margin-left: 12px;

    @media (max-width: 768px) {
      margin-left: 10px;
    }
  }

  .table-title {
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;
    margin: 0 0 2px 0;

    @media (max-width: 768px) {
      font-size: 14px;
    }
  }

  .table-subtitle {
    font-size: 13px;
    color: $text-secondary;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.title-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.title-icon {
  color: $primary-color;
  font-size: 16px;
  flex-shrink: 0;
}

.title-text {
  font-weight: 500;
  color: $text-primary;
}

.content-cell {
  max-width: 200px;
}

.content-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
  color: $text-regular;
}

.attachment-type-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  font-weight: 500;
  font-size: 12px;
  padding: 4px 8px;

  .tag-icon {
    font-size: 12px;
  }
}

.attachment-cell {
  padding: 8px;
}

.no-attachment {
  display: flex;
  align-items: center;
  gap: 6px;
  color: $text-secondary;
  font-size: 13px;
  justify-content: center;
}

.no-attachment-icon {
  font-size: 14px;
  color: $info-color;
}

.image-attachment {
  display: flex;
  justify-content: center;
}

.image-gallery {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  max-width: 280px;

  @media (max-width: 768px) {
    max-width: 200px;
    gap: 6px;
  }
}

.image-item {
  flex-shrink: 0;
}

.attachment-image {
  width: 60px;
  height: 60px;
  border-radius: $border-radius-small;
  box-shadow: $shadow-base;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.1);
    box-shadow: $shadow-light;
  }

  @media (max-width: 768px) {
    width: 50px;
    height: 50px;
  }
}

.image-error {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: $text-placeholder;
  font-size: 20px;
  border-radius: $border-radius-small;
}

.link-attachment {
  display: flex;
  justify-content: center;
}

.link-preview {
  display: flex;
  gap: 12px;
  max-width: 280px;
  padding: 12px;
  background: #fafbff;
  border-radius: $border-radius-small;
  border: 1px solid $border-light;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: $shadow-base;
  }

  @media (max-width: 768px) {
    max-width: 200px;
    gap: 8px;
    padding: 8px;
    flex-direction: column;
  }
}

.link-image {
  flex-shrink: 0;
}

.link-pic {
  width: 60px;
  height: 60px;
  border-radius: $border-radius-small;
  box-shadow: $shadow-base;

  @media (max-width: 768px) {
    width: 50px;
    height: 50px;
  }
}

.link-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.link-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: $text-primary;
  line-height: 1.4;

  span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.link-desc {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  font-size: 12px;
  color: $text-secondary;
  line-height: 1.3;

  span {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.link-icon,
.desc-icon {
  font-size: 12px;
  flex-shrink: 0;
  margin-top: 2px;

  @media (max-width: 768px) {
    font-size: 10px;
  }
}

.link-icon {
  color: $primary-color;
}

.desc-icon {
  color: $text-secondary;
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
  font-size: 12px;
  color: $text-secondary;
}

.time-icon {
  color: $text-secondary;
  font-size: 14px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 4px;
  }
}

.edit-btn,
.delete-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
  }
}

.edit-btn {
  &:hover {
    background: $primary-light;
    transform: scale(1.05);
  }
}

.delete-btn {
  &:hover {
    background: rgba(245, 108, 108, 0.1);
    transform: scale(1.05);
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .msgtemplate-container {
    padding: 4px;
  }

  .search-form-container {
    padding: 12px;
  }

  .form-actions {
    gap: 8px;

    .el-button {
      padding: 8px 12px;
      font-size: 13px;
    }
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .attachment-image {
    width: 40px;
    height: 40px;
  }

  .link-pic {
    width: 40px;
    height: 40px;
  }

  .pagination-wrapper {
    padding: 12px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .msgtemplate-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .table-header {
    background: #4b5563;
    border-color: #6b7280;
  }

  .search-title,
  .table-title {
    color: #f9fafb;
  }

  .title-text {
    color: #f9fafb;
  }

  .content-text {
    color: #d1d5db;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .link-preview {
    background: #4b5563;
    border-color: #6b7280;
  }

  .link-title {
    color: #f9fafb;
  }

  .link-desc {
    color: #d1d5db;
  }

  .image-error {
    background: #4b5563;
    color: #9ca3af;
  }
}
</style>
